package pers.lance.exam.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import pers.lance.exam.dao.BaseDao;
import pers.lance.exam.pojo.Base;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 * @ Author     ：LILA3
 * @ Date       ：Created in 1:57 PM 7/31/2018
 */
@Repository
public class BaseDapImpl implements BaseDao {
    @PersistenceContext(name = "em")
    EntityManager entityManager;

    @Transactional
    public Base add(Base base) {
        entityManager.persist(base);
        return base;
    }

    @Transactional
    public Base delete(Base base) {
        Base deleteBase = entityManager.find(Base.class, base.getId());
        entityManager.remove(deleteBase);
        return deleteBase;
    }

    public List<Base> findAll() {
        String sql = "From Base";
        return entityManager.createQuery(sql).getResultList();
    }

    public Base findById(Base base) {
        return entityManager.find(Base.class, base.getId());
    }


}
